package com.cn.bulu.TEST;

import com.alibaba.fastjson.JSON;
import com.cn.bulu.constant.CurtainConstant;
import com.cn.bulu.dao.SampleDao;
import com.cn.bulu.pojo.Goods;
import com.cn.bulu.pojo.Sample;
import com.cn.bulu.service.PriceService;
import com.cn.bulu.utils.BuluResult;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 编写并测试一次性计算10个商品的预算价格
 * @author xiaGuanghui 2017年5月25日 上午10:39:51
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath*:/spring-mybatis.xml" }) 
public class TestCalculateTenSamplePrice {
	
	@Autowired
	private SampleDao sampleDao;
	@Autowired
	private PriceService priceService;
	//测试数据
	public static final int[] sampleIds={/*1140,1089,1087,1086,1088,1090*/1044,1046,1047,1082,1084,1085,1088};
	
	//测试需要的goods(根据需要可以进行调试)
	public static Goods getGoodsDependencyNeed(){
		Goods goods = new Goods();
		//clothHigh	0
		goods.setClothHigh(0.00);
		//clothWide	0
		goods.setClothWide(0.00);
		//yarnHigh	2
		goods.setYarnHigh(2.00);
		//yarnWide	2
		goods.setYarnWide(2.00);
		//style	2
		//goods.setStyle(2);
		//structure	2
		goods.setStructure(1);
		//sampleId	1141
		goods.setSampleId(1044);
		//shadingCloth	0
		goods.setShadingCloth(2);
		
		return goods;
	}
	
	
	@Test
	public void testCalculate(){
		Goods goods = getGoodsDependencyNeed();
		BuluResult buluResult=priceService.curtainPrice(goods);
		Map<String,Object> oldmap = new HashMap<>();
		oldmap.put("goods", goods);
		oldmap.put("sampleId", goods.getSampleId());
		oldmap.put("price", buluResult.getData());
		//获取需要计算的10个商品
		List<Sample> sampleList = new ArrayList<>();
		//根据样品id获取商品的参数
		sampleList = sampleDao.selectSamplesBySampleIds(sampleIds);
		//获取传来的商品结构值
		int structure = goods.getStructure();
		buluResult = (structure==CurtainConstant.CURTAIN_STRUCTURE_CLOTHANDYARN)?
				ClothAndYarn(goods,sampleList):goingOn(structure,goods,sampleList);
		System.err.println(JSON.toJSON(oldmap));
		System.err.println((JSON.toJSONString(buluResult)));
	}
	
	/**
	 * 说明传来的goods对应sample不是structure=2的
	 * 继续判断是单纱还是单布,并根据具体指进行不同的业务计算
	 * @param structrue
	 * @param goods
	 * @param sampleList
	 * @return
	 */
	private BuluResult goingOn(int structrue, Goods goods, List<Sample> sampleList) {
		
		return structrue==CurtainConstant.CURTAIN_STRUCTURE_SINGLECLOTH?
				singleCloth(goods,sampleList):singleYarn(goods,sampleList);
	}
	
	/**
	 * 说明传来的goods对应sample是structure=0的单布类型
	 * 需要依据单布计算布纱双层或者单纱
	 * @param goods
	 * @param sampleList
	 * @return
	 */
	private BuluResult singleCloth(Goods goods, List<Sample> sampleList) {
		//参数准备
		BuluResult buluResult = new BuluResult();
		//数据准备
		List<Map<String,Object>> list = new ArrayList<>();
		for(int a=0;a<sampleList.size();a++){
			Map<String,Object> map = new HashMap<>();
			Sample sample = sampleList.get(a);
			//需要计算的商品的和传来的商品都是单布类型,直接计算
			if(sample.getStructure()==goods.getStructure()){
				Goods newGoods = getSameGoods(goods, sample);
				buluResult = priceService.curtainPrice(newGoods);
				map.put("sampleId",sample.getSampleId());
				map.put("price", buluResult.getData());
				map.put("goods", newGoods);
			//需要计算的类型是布纱双层,需要根据传来的goods参数计算布纱双层商品的价格
			}else if(sample.getStructure()==CurtainConstant.CURTAIN_STRUCTURE_CLOTHANDYARN){
				map = calculateClothAndYaraBySingleCloth(goods,sample);
			//需要计算的类型是单纱类型
			}else if(sample.getStructure()==CurtainConstant.CURTAIN_STRUCTURE_SINGLEYARN){
				map = calculateSingleYarnBySingleCloth(goods,sample);
			}else{
				map.put("sampleId",sample.getSampleId());
				map.put("price",0);
				map.put("goods", sample);
			}
			list.add(map);
		}
		buluResult.setStatus(0);
		buluResult.setData(list);
		return buluResult;
	}
	
	/**
	 * 根据单布计算单纱的价格
	 * 规定:需要计算的单纱纱尺寸和单布的尺寸一样,而需要计算的单纱商品的布尺寸规定为0
	 *     结构改为单纱
	 *     款式不进行赋值(目前自己的理解是这样的)
	 *     sampleId更改为单纱对应的商品sampleId
	 *     遮光里布和单布一样
	 * @param goods
	 * @param sample
	 * @return
	 */
	private Map<String, Object> calculateSingleYarnBySingleCloth(Goods goods, Sample sample) {
		//返回参数准备
		Map<String, Object> map = new HashMap<>();
		Goods newGoods = new Goods();
		newGoods.setClothHigh(0.00);
		newGoods.setClothWide(0.00);
		newGoods.setYarnHigh(goods.getClothHigh());
		newGoods.setYarnWide(goods.getClothWide());
		newGoods.setStructure(CurtainConstant.CURTAIN_STRUCTURE_SINGLEYARN);
		newGoods.setSampleId(sample.getSampleId());
		newGoods.setShadingCloth(goods.getShadingCloth());
		BuluResult buluResult = priceService.curtainPrice(newGoods);
		map.put("sampleId", sample.getSampleId());
		map.put("price", buluResult.getData());
		map.put("goods",newGoods);
		return map;
	}


	/**
	 * 需要根据单布计算布纱双层的价格
	 * 规定：该布纱双层的布尺寸好单布一样
	 *     纱尺寸需要重新赋值，我们默认为根据单布计算布纱双层时布的尺寸和纱的尺寸一样
	 *     款式和单布一样
	 * 	        结构改为布纱双层
	 *     遮光里布和传来的单布一样
	 *     sampleId更改为需要计算的sampleId
	 * @param goods
	 * @param sample
	 * @return
	 */
	private Map<String, Object> calculateClothAndYaraBySingleCloth(Goods goods, Sample sample) {
		//返回参数
		Map<String, Object> map = new HashMap<>();
		//创建新的goods,并按照规定来newGoods赋值
		Goods newGoods = new Goods();
		newGoods.setClothHigh(goods.getClothHigh());
		newGoods.setClothWide(goods.getClothWide());
		newGoods.setYarnHigh(goods.getClothHigh());
		newGoods.setYarnWide(goods.getClothWide());
		newGoods.setStyle(goods.getStyle());
		newGoods.setStructure(CurtainConstant.CURTAIN_STRUCTURE_CLOTHANDYARN);
		newGoods.setShadingCloth(goods.getShadingCloth());
		newGoods.setSampleId(sample.getSampleId());
		//计算价格
		BuluResult buluResult = priceService.curtainPrice(newGoods);
		map.put("sampleId",sample.getSampleId());
		map.put("price", buluResult.getData());
		map.put("goods", newGoods);
		return map;
	}


	/**
	 * 说明传来的goods对应sample是structure=1的
	 * 需要依据单纱计算单布或者布纱双层
	 * @param goods
	 * @param sampleList
	 * @return
	 */
	private BuluResult singleYarn(Goods goods, List<Sample> sampleList) {
		// 参数准备
		BuluResult buluResult = new BuluResult();
		// 数据准备
		List<Map<String, Object>> list = new ArrayList<>();
		for (int a = 0; a < sampleList.size(); a++) {
			Map<String, Object> map = new HashMap<>();
			Sample sample = sampleList.get(a);
			// 需要计算的商品的和传来的商品都是单纱类型,直接计算
			if (sample.getStructure() == goods.getStructure()) {
				Goods newGoods = getSameGoods(goods, sample);
				buluResult = priceService.curtainPrice(newGoods);
				map.put("sampleId", sample.getSampleId());
				map.put("price", buluResult.getData());
				map.put("goods", newGoods);
			// 需要计算的类型是布纱双层,需要根据传来的goods参数计算布纱双层商品的价格
			} else if (sample.getStructure() == CurtainConstant.CURTAIN_STRUCTURE_CLOTHANDYARN) {
				map = calculateClothAndYaraBySingleYarn(goods, sample);
			// 需要计算的类型是单布类型
			} else if (sample.getStructure() == CurtainConstant.CURTAIN_STRUCTURE_SINGLECLOTH) {
				map = calculateSingleClothBySingleYarn(goods, sample);
			} else {
				map.put("sampleId", sample.getSampleId());
				map.put("price", 0);
				map.put("goods", sample);
			}
			list.add(map);
		}
		buluResult.setStatus(0);
		buluResult.setData(list);
		return buluResult;
	}
	
	/**
	 * 根据单纱的属性计算单布的价格
	 * 规定:需要计算的单布商品的布尺寸和单纱的纱尺寸一样
	 * @param goods
	 * @param sample
	 * @return
	 */
	private Map<String, Object> calculateSingleClothBySingleYarn(Goods goods, Sample sample) {
		Map<String, Object> map = new HashMap<>();
		Goods newGoods = new Goods();
		//clothHigh	
		newGoods.setClothHigh(goods.getYarnHigh());
		//clothWide	
		newGoods.setClothWide(goods.getYarnWide());
		//yarnHigh	
		newGoods.setYarnHigh(goods.getClothHigh());
		//yarnWide	
		newGoods.setYarnWide(goods.getClothWide());
		//style	
		newGoods.setStyle(CurtainConstant.CURTAIN_STYLE_HANFOLDWITHHEAD);
		//structure	
		newGoods.setStructure(CurtainConstant.CURTAIN_STRUCTURE_SINGLECLOTH);
		//sampleId
		newGoods.setSampleId(sample.getSampleId());
		//shadingCloth
		newGoods.setShadingCloth(goods.getShadingCloth());
		BuluResult buluResult = priceService.curtainPrice(newGoods);
		map.put("sampleId", sample.getSampleId());
		map.put("price",buluResult.getData());
		map.put("goods", newGoods);
		return map;
	}


	/**
	 *根据单纱计算布纱双层的商品价格
	 * 规定:单纱只有纱尺寸，根据单纱计算布纱双层默认该布纱双层布的尺寸和纱的尺寸都是依据单纱的纱尺寸
	 * 	   结构改为布纱双层
	 *   款式改为韩折有帘头
	 *   更改sampleId
	 *   遮光里布和goods一样
	 * @param goods
	 * @param sample
	 * @return
	 */
	private Map<String, Object> calculateClothAndYaraBySingleYarn(Goods goods, Sample sample) {
		Map<String, Object> map = new HashMap<>();
		Goods newGoods = new Goods();
		//clothHigh	
		newGoods.setClothHigh(goods.getYarnHigh());
		//clothWide	
		newGoods.setClothWide(goods.getYarnWide());
		//yarnHigh	
		newGoods.setYarnHigh(goods.getYarnHigh());
		//yarnWide	
		newGoods.setYarnWide(goods.getYarnWide());
		//style	
		newGoods.setStyle(CurtainConstant.CURTAIN_STYLE_HANFOLDWITHHEAD);
		//structure	
		newGoods.setStructure(CurtainConstant.CURTAIN_STRUCTURE_CLOTHANDYARN);
		//sampleId
		newGoods.setSampleId(sample.getSampleId());
		//shadingCloth
		newGoods.setShadingCloth(goods.getShadingCloth());
		BuluResult buluResult = priceService.curtainPrice(newGoods);
		map.put("sampleId", sample.getSampleId());
		map.put("price",buluResult.getData());
		map.put("goods", newGoods);
		return map;
	}


	/**
	 * 说明传来的goods对应sample是structure=2的
	 * 需要根据布纱双层计算单布或者单纱
	 * @param goods
	 * @param sampleList
	 * @return
	 */
	private BuluResult ClothAndYarn(Goods goods, List<Sample> sampleList) {
		BuluResult buluResult = new BuluResult();
		List<Map<String,Object>> samplePrice = new ArrayList<>();
		for(int a=0;a<sampleList.size();a++){
			Sample sample = sampleList.get(a);
			Map<String,Object> map = new HashMap<>();
			//需要计算的商品和传来的商品结构都是2
			if(sample.getStructure()==goods.getStructure()){
				Goods newGoods = getSameGoods(goods,sample);
				buluResult = priceService.curtainPrice(newGoods);
				map.put("sampleId", sample.getSampleId());
				map.put("price", buluResult.getData());
				map.put("goods", newGoods);
			//需要计算的商品结构为单布0
			}else if(sample.getStructure()==CurtainConstant.CURTAIN_STRUCTURE_SINGLECLOTH){
				map = calculateSingleClothByClothAndYarn(goods,sample);
			//需要计算的商品结构为单纱1
			}else if(sample.getStructure()==CurtainConstant.CURTAIN_STRUCTURE_SINGLEYARN){
				map = calculateSingleYarnByClothAndYarn(goods,sample);
			}else{
				map.put("sampleId", sample.getSampleId());
				map.put("price", 0);
				map.put("goods", sample);
			}
			samplePrice.add(map);
		}
		buluResult.setStatus(0);
		buluResult.setData(samplePrice);
		return buluResult;
	}
	
	/**
	 * 根据传来的goods对象进行计算相同类型的sample的价格
	 * goods对应的sampleId还是需要更换为需要计算的sampleId的值
	 * @param goods
	 * @param sample
	 * @return
	 */
	private Goods getSameGoods(Goods goods, Sample sample) {
		Goods newGoods = new Goods();
		//clothHigh
		newGoods.setClothHigh(goods.getClothHigh());
		//clothWide
		newGoods.setClothWide(goods.getClothWide());
		//yarnHigh	
		newGoods.setYarnHigh(goods.getYarnHigh());
		//yarnWide	
		newGoods.setYarnWide(goods.getYarnWide());
		//style	
		if(/*goods.getStructure()!=1*/sample.getStructure()!=1){
			newGoods.setStyle(goods.getStyle());
		}
		//structure	
		newGoods.setStructure(goods.getStructure());
		//sampleId	
		newGoods.setSampleId(sample.getSampleId());
		//shadingCloth
		newGoods.setShadingCloth(goods.getShadingCloth());
		return newGoods;
	}

	/**
	 * 根据布纱双层计算单纱商品的预算价格
	 * 规定：关于布的尺寸设置为0
	 * 	         结构值=1
	 * 	         单布的遮光里布和传来的goods一样
	 *     单纱目前没有款式(款式指style的值)
	 *     关于单布的布尺寸和传来的goods一样(如果传来的goods没有纱尺寸就将该单布尺寸设置为传来goods的布尺寸)
	 * @param goods
	 * @param sample
	 * @return
	 */
	private Map<String,Object> calculateSingleClothByClothAndYarn(Goods goods, Sample sample) {
		Map<String,Object> map = new HashMap<>();
		Goods newGoods = new Goods();
		newGoods.setSampleId(sample.getSampleId());
		newGoods.setClothHigh(0.00);
		newGoods.setClothHigh(0.00);
		newGoods.setStructure(CurtainConstant.CURTAIN_STRUCTURE_SINGLEYARN);
		newGoods.setShadingCloth(goods.getShadingCloth());
		if(goods.getYarnHigh()==0||goods.getYarnWide()==0){
			newGoods.setYarnHigh(goods.getClothHigh());
			newGoods.setYarnWide(goods.getClothWide());
		}else{
			newGoods.setYarnHigh(goods.getYarnHigh());
			newGoods.setYarnWide(goods.getYarnWide());
		}
		BuluResult buluResult = priceService.curtainPrice(newGoods);
		map.put("goods", newGoods);
		map.put("sampleId", sample.getSampleId());
		map.put("price", buluResult.getData());
		return map;
	}

	/**
	 * 根据布纱双层计算单布商品预算价格 
	 * 规定：关于纱的尺寸设置为0
	 * 	         结构值=0
	 * 	         单布的遮光里布和传来的goods一样
	 * 	         单布的款式和传来的goods一样
	 *      关于单布的布尺寸和传来的goods一样(如果传来的goods没有布尺寸就将该单布尺寸设置为传来goods的纱尺寸)
	 * @param goods
	 * @param sample
	 * @return
	 */
	private Map<String,Object> calculateSingleYarnByClothAndYarn(Goods goods, Sample sample) {
		Map<String,Object> map = new HashMap<>();
		Goods newGoods = new Goods();
		newGoods.setSampleId(sample.getSampleId());
		newGoods.setYarnHigh(0.00);
		newGoods.setYarnWide(0.00);
		newGoods.setStructure(CurtainConstant.CURTAIN_STRUCTURE_SINGLECLOTH);
		newGoods.setShadingCloth(goods.getShadingCloth());
		newGoods.setStyle(goods.getStyle());
		if(goods.getClothHigh()==0||goods.getClothWide()==0){
			newGoods.setClothHigh(goods.getYarnHigh());
			newGoods.setClothWide(goods.getYarnWide());
		}else{
			newGoods.setClothHigh(goods.getClothHigh());
			newGoods.setClothWide(goods.getClothWide());
		}
		BuluResult buluResult = priceService.curtainPrice(newGoods);
		map.put("goods", newGoods);
		map.put("sampleId", sample.getSampleId());
		map.put("price", buluResult.getData());
		return map;
	}
	
	
	
	
	
	
	
	
}
